<?php
/**
 * @ClassName: Equipment
 * @Description: 设备管理
 * @author: gydtrade
 * @Date: 2025-11-13 14:27
 * @Version: V1.0.0
 */

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class Equipment extends Model
{
    use SoftDelete;

    protected $name = 'equipment';

    protected $autoWriteTimestamp = 'dateTime';

    protected $append = ['device_type_str', 'holder_info', 'use_info'];

    const sales_method_none = -1;
    const sales_method_sell = 1;
    const sales_method_rent = 2;
    const sales_method_borrow = 3;

    const holder_type_platform = 1;
    const holder_type_center = 2;
    const holder_type_agent = 3;
    const holder_type_store = 4;

    const use_type_platform = 1;
    const use_type_center = 2;
    const use_type_agent = 3;
    const use_type_store = 4;
    const use_type_customer = 5;
    const use_type_user = 6;

    public static $sales_methods = [
        self::sales_method_none => '无',
        self::sales_method_sell => '卖',
        self::sales_method_rent => '租',
        self::sales_method_borrow => '借',
    ];

    public static $holder_types = [
        self::holder_type_platform => '平台',
        self::holder_type_center => '运营中心',
        self::holder_type_agent => '代理',
        self::holder_type_store => '商户',
    ];

    public static $use_types = [
        self::use_type_platform => '平台',
        self::use_type_center => '运营中心',
        self::use_type_agent => '代理',
        self::use_type_store => '商户',
        self::use_type_customer => '客户',
        self::use_type_user => '用户',
    ];

    public function getDeviceTypeStrAttr($value, $data)
    {
        $res = EquipmentType::get($data['device_type']);
        if ($res) {
            return $res['name'];
        }

        return $data['device_type'];
    }

    public function getHolderInfoAttr($value, $data)
    {
        switch ($data['holder_type']) {
            case 2:
                // 运营中心
                $res = OperationCenter::get($data['h_related_id']);
                if ($res) {
                    $text = "运营中心：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "运营中心：" . $data['h_related_id'];
                }
                break;
            case 3:
                // 代理
                $res = Agent::get($data['h_related_id']);
                if ($res) {
                    $text = "代理：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "代理：" . $data['h_related_id'];
                }
                break;
            case 4:
                // 门店
                $res = Store::get($data['h_related_id']);
                if ($res) {
                    $text = "门店：" . $res['id'] . "#" . $res['store_name'];
                } else {
                    $text = "门店：" . $data['h_related_id'];
                }
                break;
            default:
                // 平台
                $text = '平台';
                break;
        }

        return $text;
    }

    public function getUseInfoAttr($value, $data)
    {
        switch ($data['use_type']) {
            case 2:
                // 运营中心
                $res = OperationCenter::get($data['u_related_id']);
                if ($res) {
                    $text = "运营中心：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "运营中心：" . $data['u_related_id'];
                }
                break;
            case 3:
                // 代理
                $res = Agent::get($data['u_related_id']);
                if ($res) {
                    $text = "代理：" . $res['id'] . "#" . $res['name'];
                } else {
                    $text = "代理：" . $data['u_related_id'];
                }
                break;
            case 4:
                // 门店
                $res = Store::get($data['u_related_id']);
                if ($res) {
                    $text = "门店：" . $res['id'] . "#" . $res['store_name'];
                } else {
                    $text = "门店：" . $data['u_related_id'];
                }
                break;
            case 5:
                // 客户
                $res = Customer::get($data['u_related_id']);
                if ($res) {
                    $text = "客户：" . $res['id'] . "#" . $res['customer_name'];
                } else {
                    $text = "客户：" . $data['u_related_id'];
                }
                break;
            case 6:
                // 会员
                $res = Member::get($data['u_related_id']);
                if ($res) {
                    $text = "会员：" . $res['id'] . "#" . $res['nickname'];
                } else {
                    $text = "会员：" . $data['u_related_id'];
                }
                break;
            default:
                // 无
                $text = '无';
                break;
        }

        return $text;
    }

}